package lagou.homework.lagou_homework_rocketmq.controller;

import lagou.homework.lagou_homework_rocketmq.entity.SeckillVo;
import lagou.homework.lagou_homework_rocketmq.repository.SeckillVoRepository;
import lagou.homework.lagou_homework_rocketmq.utils.Response;
import lagou.homework.lagou_homework_rocketmq.common.ResponseResult;
import lagou.homework.lagou_homework_rocketmq.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private SeckillVoRepository seckillVoRepository;

    /**
     * 秒杀
     * @param userId
     * @param seckillId
     * @return
     */
    @RequestMapping("/kill")
    @ResponseBody
    public ResponseResult kill(String userId, String seckillId){
        String orderNo = seckillService.kill(userId,seckillId);
        return Response.success(""+userId+"秒杀成功，请尽快下单",orderNo);
    }

    /**
     * 重置秒杀
     * @param seckillId
     * @return
     */
    @RequestMapping("/reset")
    @ResponseBody
    public ResponseResult reset(String seckillId){
        seckillService.reset(seckillId);
        return Response.success("重置秒杀成功",null);
    }

    /**
     * 加载秒杀
     * @return
     */
    @RequestMapping("/list")
    public String toKill(Model model,String userId){
        List<SeckillVo> seckillVos = seckillVoRepository.loadSeckillVoList();
        model.addAttribute("seclist",seckillVos);
        model.addAttribute("userId",userId);
        return "kill_list";
    }

}
